package StudyCode.summary.Basic.console;

import java.util.Locale;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class ProgressBar {
	public static void main(String[] args) {
		System.out.println(getCmdProgressBar(0.1f));
		System.out.println(getCmdProgressBar(0.5f));
		System.out.println(getCmdProgressBar(1.0f));
	}

	private static String getCmdProgressBar(float progress) {
		int loadingBarCount = 40;
		int bars = Math.round((float) loadingBarCount * progress);
		StringBuilder sb = new StringBuilder("\r[");

		for (int i = 0; i < loadingBarCount; i++) {
			if (i < bars) {
				sb.append("-");
			} else {
				sb.append(" ");
			}
		}
		sb.append("] ");

		if (progress < 1f) {
			sb.append(String.format("%6s", String.format(Locale.US, "%.2f", progress * 100f))).append("%");
		} else {
			sb.append("100.00%\n");
		}

		return sb.toString();
	}
}
